package com.exception;

import com.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

//统一异常处理
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result doException(Exception e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(500, "系统维护中，稍后再试", null);
    }
    @ExceptionHandler
    public Result doRuntimeException(RuntimeException e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(502, e.getMessage(), null);
    }
    @ExceptionHandler(UsernameOrPasswordWrongException.class)
    public Result doUsernameOrPasswordWrongException(UsernameOrPasswordWrongException e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(201, e.getMessage(), null);
    }

    @ExceptionHandler(SupplierUsedException.class)
    public Result doSupplierUsedException(SupplierUsedException e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(301, e.getMessage(), null);
    }

    //  多重校验
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result doMethodArgumentNotValidException(MethodArgumentNotValidException e){
        BindingResult result = e.getBindingResult();
        Map<String, String> map = new HashMap<>();
        if (result.getErrorCount() > 0) {
            result.getFieldErrors().forEach(item -> {
                map.put(item.getField(), item.getDefaultMessage());
            });
        }
        return new Result(100, "数据验证失败",null);
    }
    @ExceptionHandler(RawsLackException.class)
    public Result doRawsLackExceptionException(RawsLackException e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(500, "原料不足", null);
    }
    @ExceptionHandler(UnpaidStorageException.class)
    public Result doUnpaidStorageException(UnpaidStorageException e){
        e.printStackTrace();
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(302, e.getMessage(), null);
    }

    @ExceptionHandler(ClientException.class)
    public Result doClientException(ClientException e){
        log.info("exception-class:" + e.getClass());
        log.info("exception-class:" + e.getMessage());
        return new Result(202, e.getMessage(), null);
    }
}
